<template>
  <el-cascader
    :value="value"
    size="mini"
    :options="depts"
    :props="defaultProps"
    separator="-"
    @change="handleSelect"
  />
</template>

<script>
import { getDepartmentList } from '@/api/department'
import { totree } from '@/utils'

export default {
  props: {
    value: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      depts: [],
      defaultProps: {
        value: 'id',
        label: 'name'
      }
    }
  },
  created() {
    this.getDepts()
  },
  methods: {
    async getDepts() {
      const res = await getDepartmentList()
      this.depts = totree(res, 0)
    },
    // 选择 取数组最后一项的id
    handleSelect(list) {
      if (list.length > 0) {
        this.$emit('input', list[list.length - 1])
      } else {
        this.$emit('input', null)
      }
    }
  }
}
</script>

<style scoped lang='scss'></style>
